<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\BaseController;
use App\Http\Controllers\Controller;
use App\Http\Requests\CommentsRequest;
use App\Models\Comments;
use App\Models\Order;
use Illuminate\Http\Request;

class CommentsController extends BaseController
{
    /**
     * 评论
     */
    public function store(CommentsRequest $request,Order $order)
    {
        // $request->offsetSet('users_id',$request->userId);
        
        $order_id= $request->input('order_id');
        $request->merge(['users_id'=>request()->userId]);
        $order= Order::where('id',$order_id)->where('users_id',request()->userId)->first();
        
        if($order->status==10) return $this->apiError('訂單未付款！');
    

        Comments::create($request->all());
        Order::where('id',$order_id)->where('users_id',request()->userId)->update(['comment'=>1]);
        return $this->success();
        // if(in_array($request->input('products_id'),array_column($order->product_item,'id'))){
        //     // $request->except('order_id');
        //     Comments::create($request->all());
        //     Order::where('id',$order_id)->where('users_id',request()->userId)->update(['comment'=>1]);
        //     return $this->success();
        // }else{
        //     return $this->apiError('該商品不存在該訂單');
        // }
        
    
    }
}
